package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.BaseControl;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.base.Rs;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/file")
public class FileController extends BaseControl {

    @Resource
    IFileService fileService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/upload")
    public Rs<Object> uploadFile(MultipartFile file, @RequestParam(required = false) String name) throws Exception {
//        getApiUser();
        return Rs.ok(fileService.uploadFile(file, name));
    }

    @DeleteMapping("/{id}")
    public Rs<Object> deleteFile(@PathVariable String id) throws BusinessException {
        commonService.getApiUser();
        fileService.deleteFile(id);
        return Rs.ok();
    }

    @GetMapping("/{id}")
    public void downloadFile(HttpServletResponse response, @PathVariable String id) throws Exception {
        fileService.downloadFile(response, id);
    }

    @PostMapping("/build/qrcode")
    public void buildQRCode(HttpServletResponse response, @RequestBody String content) throws Exception {
        fileService.buildQRCode(response, content);
    }

}
